﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows;
using System.Windows.Shapes;
using System.Windows.Media.Animation;

namespace Zero.UControl.Component
{
    public class RippleButton : Button
    {
        static RippleButton()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(RippleButton), new FrameworkPropertyMetadata(typeof(RippleButton)));
        }

        // 按钮类型属性
        public ButtonType BtnType
        {
            get => (ButtonType)GetValue(BtnTypeProperty);
            set => SetValue(BtnTypeProperty, value);
        }

        public static readonly DependencyProperty BtnTypeProperty =
            DependencyProperty.Register(nameof(BtnType), typeof(ButtonType), typeof(RippleButton),
                new PropertyMetadata(ButtonType.PRIMARY, OnBtnTypeChanged));

        private static void OnBtnTypeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var button = (RippleButton)d;
            button.UpdateBackground();
        }

        private static readonly Dictionary<ButtonType, string> BkColorDict = new()
        {
            { ButtonType.PRIMARY,"#007ac1" },
            { ButtonType.SUCCESS, "#67c23a" },
            { ButtonType.WARNING, "#e6a23c" },
            { ButtonType.ERROR, "#ff0000" },
        };

        private void UpdateBackground()
        {
            if (Template?.FindName("border", this) is Border border)
            {
                border.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString(BkColorDict[BtnType]));
            }
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            var canvas = GetTemplateChild("RippleCanvas") as Canvas;
            if (canvas != null)
            {
                PreviewMouseLeftButtonDown += (s, e) =>
                {
                    var clickPos = e.GetPosition(canvas);
                    double maxRadius = Math.Max(ActualWidth, ActualHeight) * 1.5;

                    var ripple = new Ellipse
                    {
                        Width = 5,
                        Height = 5,
                        Fill = new SolidColorBrush(Color.FromArgb(64, 255, 255, 255)),
                        Opacity = 1,
                        RenderTransformOrigin = new Point(0.5, 0.5),
                        RenderTransform = new ScaleTransform(1, 1)
                    };

                    Canvas.SetLeft(ripple, clickPos.X - ripple.Width / 2);
                    Canvas.SetTop(ripple, clickPos.Y - ripple.Height / 2);
                    canvas.Children.Add(ripple);

                    var scaleAnim = new DoubleAnimation(1, maxRadius / ripple.Width, TimeSpan.FromSeconds(0.5))
                    {
                        EasingFunction = new CircleEase { EasingMode = EasingMode.EaseOut }
                    };
                    var opacityAnim = new DoubleAnimation(1, 0, TimeSpan.FromSeconds(0.5));

                    var transform = (ScaleTransform)ripple.RenderTransform;
                    transform.BeginAnimation(ScaleTransform.ScaleXProperty, scaleAnim);
                    transform.BeginAnimation(ScaleTransform.ScaleYProperty, scaleAnim);
                    ripple.BeginAnimation(OpacityProperty, opacityAnim);

                    opacityAnim.Completed += (s2, e2) => canvas.Children.Remove(ripple);
                };
            }

            UpdateBackground(); // 初始化背景色
        }
    }

    public enum ButtonType
    {
        PRIMARY,
        SUCCESS,
        WARNING,
        ERROR
    }

}
